package com.yohann.course_service.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yohann.course_service.entity.Subject;
import com.yohann.course_service.service.SubjectService;
import com.yohann.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程分类 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-10-18
 */
@Api(tags = "课程分类")
@RestController
@RequestMapping("/course_service/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    /**
     * 根据id查询课程分类
     *
     * @param id 分类id
     * @return 返回对应分类
     */
    @ApiOperation("根据id查询课程分类")
    @GetMapping("getSubjectById/{id}")
    public Result getSubjectById(@ApiParam("分类id") @PathVariable int id) {
        Subject subject = subjectService.getById(id);

        return Result.ok().data("subject", subject);
    }

    /**
     * 分页查询课程分类
     *
     * @param current 起始位置
     * @param limit   结束位置
     * @return 返回分类结果集
     */
    @ApiOperation("分页查询课程分类")
    @GetMapping("getSubject/{current}/{limit}")
    public Result pageQuerySubject(@ApiParam("起始位置") @PathVariable long current, @ApiParam("结束位置") @PathVariable long limit) {
        Page<Subject> page = new Page<>(current, limit);
        Map<String, Object> map = subjectService.pageQuerySubject(page);

        return Result.ok().data(map);
    }

    /**
     * 查询所有课程分类
     *
     * @return 返回分类结果集
     */
    @ApiOperation("查询所有课程分类")
    @GetMapping("getAllSubject")
    public Result getAllSubject() {
        List<Subject> list = subjectService.list(null);

        return Result.ok().data("list", list);
    }

    /**
     * 添加课程分类
     *
     * @param title 分类名称
     * @return 添加状态
     */
    @ApiOperation("添加课程分类")
    @GetMapping("addSubject/{title}")
    public Result addSubject(@ApiParam("分类名称") @PathVariable String title) {
        Subject subject = new Subject();
        subject.setTitle(title);

        boolean save = subjectService.save(subject);

        return Result.state(save);
    }

    /**
     * 修改分类
     *
     * @param subject 分类信息
     * @return 修改状态
     */
    @ApiOperation("修改分类")
    @PostMapping("updateSubject")
    public Result updateSubject(@ApiParam("分类信息") @RequestBody Subject subject) {
        boolean update = subjectService.updateById(subject);

        return Result.state(update);
    }

    /**
     * 删除课程分类
     *
     * @param id 分类id
     * @return 删除状态
     */
    @ApiOperation("删除课程分类")
    @GetMapping("deleteSubject/{id}")
    public Result deleteSubject(@ApiParam("分类id") @PathVariable int id) {
        boolean remove = subjectService.removeById(id);

        return Result.state(remove);
    }
}

